using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Common.NotifyEvent;
using SExperiment;

namespace SWellPlate
{
	public partial class UCWellTargetEditor : UserControl, IComponentConnector
	{
		public static readonly DependencyProperty ProjectTypeProperty = DependencyProperty.Register("ProjectType", typeof(EProjectType), typeof(UCWellTargetEditor), new UIPropertyMetadata(EProjectType.AQ, OnProjectTypeChanged));

		public static readonly DependencyProperty UseCompoundTubeNoProperty = DependencyProperty.Register("UseCompoundTubeNo", typeof(bool), typeof(UCWellTargetEditor), new UIPropertyMetadata(false));

		public static readonly DependencyProperty SampleNameProperty = DependencyProperty.Register("SampleName", typeof(string), typeof(UCWellTargetEditor), new UIPropertyMetadata(null));

		public static readonly DependencyProperty CompoundTubeNoProperty = DependencyProperty.Register("CompoundTubeNo", typeof(string), typeof(UCWellTargetEditor), new UIPropertyMetadata(null));

		public static readonly DependencyProperty SSampleTypeProperty = DependencyProperty.Register("SSampleType", typeof(object), typeof(UCWellTargetEditor), new UIPropertyMetadata(null));

		public static readonly DependencyProperty PropertyProperty = DependencyProperty.Register("Property", typeof(string), typeof(UCWellTargetEditor), new UIPropertyMetadata(null));

		public static readonly DependencyProperty CanModifyProperty = DependencyProperty.Register("CanModify", typeof(bool), typeof(UCWellTargetEditor), new UIPropertyMetadata(true, OnResultColumnsChanged));

		private bool _IsSetValue = true;

		private IList<Well> SelectedWells;

		private Sample Sample;

		private bool bUpdate = true;

		private bool canInputWord;

		public EProjectType ProjectType
		{
			get
			{
				return (EProjectType)GetValue(ProjectTypeProperty);
			}
			set
			{
				SetValue(ProjectTypeProperty, value);
			}
		}

		public bool UseCompoundTubeNo
		{
			get
			{
				return (bool)GetValue(UseCompoundTubeNoProperty);
			}
			set
			{
				SetValue(UseCompoundTubeNoProperty, value);
			}
		}

		public string SampleName
		{
			get
			{
				return (string)GetValue(SampleNameProperty);
			}
			set
			{
				SetValue(SampleNameProperty, value);
			}
		}

		public string CompoundTubeNo
		{
			get
			{
				return (string)GetValue(CompoundTubeNoProperty);
			}
			set
			{
				SetValue(CompoundTubeNoProperty, value);
			}
		}

		public object SSampleType
		{
			get
			{
				return GetValue(SSampleTypeProperty);
			}
			set
			{
				SetValue(SSampleTypeProperty, value);
			}
		}

		public string Property
		{
			get
			{
				return (string)GetValue(PropertyProperty);
			}
			set
			{
				SetValue(PropertyProperty, value);
			}
		}

		public bool CanModify
		{
			get
			{
				return (bool)GetValue(CanModifyProperty);
			}
			set
			{
				SetValue(CanModifyProperty, value);
			}
		}

		public bool IsSetValue
		{
			get
			{
				return _IsSetValue;
			}
			set
			{
				if (_IsSetValue != value)
				{
					_IsSetValue = value;
				}
			}
		}

		public List<KeyValuePair> AllSampleType
		{
			get
			{
				return EnumManagement.GetAllSampleType();
			}
		}

		private static void OnProjectTypeChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCWellTargetEditor uCWellTargetEditor = sender as UCWellTargetEditor;
		}

		private static void OnResultColumnsChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			UCWellTargetEditor uCWellTargetEditor = sender as UCWellTargetEditor;
			if (uCWellTargetEditor != null)
			{
				uCWellTargetEditor.IsEnabled = uCWellTargetEditor.CanModify;
			}
		}

		public UCWellTargetEditor()
		{
			InitializeComponent();
			AddUnionCtrlEvent();
		}

		public void SetSelectedSample(Sample sample)
		{
			if (sample != null)
			{
				Sample = sample;
				SetDataContent(Sample);
			}
		}

		public void SetSelectedSample(IList<Well> list)
		{
			Sample = null;
			SelectedWells = list;
			if (list == null)
			{
				SetDataContent(null);
				return;
			}
			if ((from s in list
				where s.Project != null
				select s.Project).Distinct().Count() == 1)
			{
				foreach (Well selectedWell in SelectedWells)
				{
					if (selectedWell.Sample != null)
					{
						if (Sample == null)
						{
							Sample = selectedWell.Sample.CreateBasicSample();
						}
						else
						{
							Sample = GetSameSample(Sample, selectedWell.Sample);
						}
						Sample.Project = selectedWell.Project;
					}
				}
			}
			SetDataContent(Sample);
		}

		private Sample GetSameSample(Sample first, Sample second)
		{
			Sample result = null;
			if (first == null || second == null)
			{
				return result;
			}
			if (first.Items.Count != second.Items.Count)
			{
				return result;
			}
			result = first.CreateBasicSample();
			for (int i = 0; i < first.Items.Count; i++)
			{
				if (first.Items[i].SampleType != second.Items[i].SampleType)
				{
					result.Items[i].SampleType = SampleType.Last;
				}
				if (first.Items[i].Property != second.Items[i].Property)
				{
					result.Items[i].Property = null;
				}
				if (first.Items[i].Name != second.Items[i].Name)
				{
					result.Items[i].Name = null;
				}
			}
			if (result != null)
			{
				if (first.CompoundTubeNo != second.CompoundTubeNo)
				{
					result.CompoundTubeNo = null;
				}
				if (first.SampleName != second.SampleName)
				{
					result.SampleName = null;
				}
			}
			return result;
		}

		private void SetDataContent(Sample sample)
		{
			RemoveUnionCtrlEvent();
			canInputWord = false;
			if (sample == null)
			{
				SSampleType = null;
				Property = null;
				gridControlTarget.ItemsSource = null;
				UseCompoundTubeNo = false;
				CompoundTubeNo = null;
				base.IsEnabled = false;
				SampleName = null;
				AddUnionCtrlEvent();
				return;
			}
			base.IsEnabled = CanModify;
			gridControlTarget.ItemsSource = sample.Items;
			UseCompoundTubeNo = !string.IsNullOrEmpty(Sample.CompoundTubeNo);
			CompoundTubeNo = sample.CompoundTubeNo;
			textCompoundTubeNo.IsEnabled = UseCompoundTubeNo;
			SampleName = sample.SampleName;
			UpdateUnionData();
			foreach (SampleTargetItem item in Sample.Items)
			{
				item.Event += item_Event;
			}
			if (sample.Project != null)
			{
				switch (sample.Project.BasicInfo.Type)
				{
				case EProjectType.AQ:
				case EProjectType.RQ:
				case EProjectType.TQ:
					canInputWord = false;
					break;
				default:
					canInputWord = true;
					break;
				}
			}
		}

		private void AddUnionCtrlEvent()
		{
			cmbSampleType.SelectionChanged += cmbSampleType_SelectionChanged;
			textProperty.TextChanged += textProperty_TextChanged;
			textCompoundTubeNo.TextChanged += textCompoundTubeNo_TextChanged;
			textSample_name.TextChanged += textSample_name_TextChanged;
		}

		private void RemoveUnionCtrlEvent()
		{
			cmbSampleType.SelectionChanged -= cmbSampleType_SelectionChanged;
			textProperty.TextChanged -= textProperty_TextChanged;
			textCompoundTubeNo.TextChanged -= textCompoundTubeNo_TextChanged;
			textSample_name.TextChanged -= textSample_name_TextChanged;
		}

		private void textCompoundTubeNo_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (SelectedWells == null || Sample == null || !_IsSetValue)
			{
				return;
			}
			foreach (Well selectedWell in SelectedWells)
			{
				if (selectedWell.Sample != null)
				{
					selectedWell.Sample.CompoundTubeNo = ((CompoundTubeNo == null) ? "" : CompoundTubeNo);
				}
			}
		}

		private void textSample_name_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (SelectedWells == null || Sample == null || !_IsSetValue)
			{
				return;
			}
			foreach (Well selectedWell in SelectedWells)
			{
				if (selectedWell.Sample != null)
				{
					selectedWell.Sample.SampleName = ((SampleName == null) ? "" : SampleName);
				}
			}
		}

		private void item_Event(object sender, EventArgs e)
		{
			PropertyChangedEventArgsEx propertyChangedEventArgsEx = e as PropertyChangedEventArgsEx;
			if (propertyChangedEventArgsEx == null || (!(propertyChangedEventArgsEx.PropertyName == "SampleType") && !(propertyChangedEventArgsEx.PropertyName == "Property") && !(propertyChangedEventArgsEx.PropertyName == "Name") && !(propertyChangedEventArgsEx.PropertyName == "LineColor")))
			{
				return;
			}
			if (propertyChangedEventArgsEx.PropertyName != "Name")
			{
				UpdateUnionData();
			}
			SampleTargetItem sampleTargetItem = sender as SampleTargetItem;
			if (!_IsSetValue)
			{
				foreach (SampleTargetItem item in Sample.Items)
				{
					if (item.ChannelNo == sampleTargetItem.ChannelNo && item.TubeNo == sampleTargetItem.TubeNo)
					{
						if (!ValidateProperty(item.SampleType, sampleTargetItem.Property))
						{
							sampleTargetItem.Property = item.Property;
							return;
						}
						item.Property = sampleTargetItem.Property;
					}
				}
			}
			else
			{
				foreach (Well selectedWell in SelectedWells)
				{
					if (selectedWell.Sample == null)
					{
						continue;
					}
					foreach (SampleTargetItem item2 in selectedWell.Sample.Items)
					{
						if (item2.ChannelNo != sampleTargetItem.ChannelNo || item2.TubeNo != sampleTargetItem.TubeNo)
						{
							continue;
						}
						if (propertyChangedEventArgsEx.PropertyName == "SampleType")
						{
							item2.SampleType = sampleTargetItem.SampleType;
						}
						else if (propertyChangedEventArgsEx.PropertyName == "Property")
						{
							if (!ValidateProperty(item2.SampleType, sampleTargetItem.Property))
							{
								sampleTargetItem.Property = item2.Property;
								return;
							}
							item2.Property = sampleTargetItem.Property;
						}
						else if (propertyChangedEventArgsEx.PropertyName == "Name")
						{
							item2.Name = sampleTargetItem.Name;
						}
					}
				}
			}
			if (propertyChangedEventArgsEx.PropertyName == "Name")
			{
				foreach (SampleTargetItem item3 in Sample.Items)
				{
					item3.Name = sampleTargetItem.Name;
				}
			}
			if (propertyChangedEventArgsEx.PropertyName == "SampleType" && !canInputWord)
			{
				if (sampleTargetItem.SampleType == SampleType.Standard)
				{
					sampleTargetItem.Property = "100000";
				}
				else
				{
					sampleTargetItem.Property = null;
				}
			}
		}

		private void UpdateUnionData()
		{
			if (Sample != null && bUpdate)
			{
				RemoveUnionCtrlEvent();
				IList<SampleType> list = Sample.Items.Select((SampleTargetItem s) => s.SampleType).Distinct().ToList();
				if (list.Count == 1)
				{
					SSampleType = list[0];
				}
				else
				{
					SSampleType = null;
				}
				IList<string> list2 = Sample.Items.Select((SampleTargetItem s) => s.Property).Distinct().ToList();
				if (list2.Count == 1)
				{
					Property = list2[0];
				}
				else
				{
					Property = "";
				}
				AddUnionCtrlEvent();
			}
		}

		private void checkCompoundTube_Unchecked(object sender, RoutedEventArgs e)
		{
			if (Sample != null)
			{
				CompoundTubeNo = "";
				textCompoundTubeNo.IsEnabled = false;
			}
		}

		private void checkCompoundTube_Checked(object sender, RoutedEventArgs e)
		{
			if (Sample != null)
			{
				textCompoundTubeNo.IsEnabled = true;
			}
		}

		private void cmbSampleType_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (Sample == null || !_IsSetValue)
			{
				return;
			}
			bUpdate = false;
			foreach (SampleTargetItem item in Sample.Items)
			{
				if (SSampleType != null)
				{
					item.SampleType = (SampleType)SSampleType;
				}
			}
			bUpdate = true;
			UpdateUnionData();
		}

		private void textProperty_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (Sample == null || SSampleType == null)
			{
				return;
			}
			if (!canInputWord && !ValidateProperty((SampleType)SSampleType, Property))
			{
				textProperty.TextChanged -= textProperty_TextChanged;
				TextChange[] array = new TextChange[e.Changes.Count];
				e.Changes.CopyTo(array, 0);
				int offset = array[0].Offset;
				if (array[0].AddedLength > 0)
				{
					Property = Property.Remove(offset, array[0].AddedLength);
				}
				else if (Property.Length == 0)
				{
					Property = Sample.Items[0].Property;
				}
				textProperty.SelectAll();
				textProperty.TextChanged += textProperty_TextChanged;
				return;
			}
			bUpdate = false;
			foreach (SampleTargetItem item in Sample.Items)
			{
				item.Property = ((Property == null) ? "" : Property);
			}
			bUpdate = true;
		}

		public void SetResult()
		{
			foreach (Well selectedWell in SelectedWells)
			{
				if (selectedWell.Sample == null)
				{
					continue;
				}
				selectedWell.Sample.CompoundTubeNo = ((CompoundTubeNo == null) ? "" : CompoundTubeNo);
				selectedWell.Sample.SampleName = ((SampleName == null) ? "" : SampleName);
				foreach (SampleTargetItem item in selectedWell.Sample.Items)
				{
					ObservableCollection<SampleTargetItem> items = Sample.Items;
					Func<SampleTargetItem, bool> predicate = (SampleTargetItem s) => s.TubeNo == item.TubeNo && s.ChannelNo == item.ChannelNo;
					if (items.Where(predicate).Count() > 0)
					{
						SampleTargetItem sampleTargetItem = Sample.Items.Where((SampleTargetItem s) => s.TubeNo == item.TubeNo && s.ChannelNo == item.ChannelNo).FirstOrDefault();
						item.Property = sampleTargetItem.Property;
					}
					if (SSampleType != null)
					{
						item.SampleType = (SampleType)SSampleType;
					}
				}
			}
		}

		private bool ValidateProperty(SampleType type, string property)
		{
			if (!canInputWord && type == SampleType.Standard)
			{
				double result = 0.0;
				if (!double.TryParse(property, out result))
				{
					MessageBox.Show(Utility.GetMessage("msg_please_input_num"));
					return false;
				}
			}
			return true;
		}
	}
}
